import { math } from "cc";
import * as fgui from "fairygui-cc";
import { stat } from "fs";
import { ModelGameRobot } from "../../../model/ModelGameRobot";
import { Grid } from "./Grid";
import EventMgr from "../../../../../Core/Event/EventMgr";
import { MathUtils } from "../../../../../Core/Utils/MathUtils";
export class Plane extends fgui.GComponent {
    private ctrl_state: fgui.Controller;
    private rotate: fgui.GButton;
    private gridList: Grid[];
    public gridArray: string[];
    public fristgrid: string;
    private _px: number;
    private _py: number;
    private _ppx: number;
    private _ppy: number;
    private _pw: number;
    private _ph: number;
    protected onConstruct(): void {
        this.ctrl_state = this.getController("ctrl_state");
        this.rotate = this.getChild("rotate");
        this.rotate.onClick(this.onRotateClick, this)
        this.gridList = [];
        for (let i = 0; i < 10; i++) {
            this.gridList.push(this.getChild("grid" + i));
            this.gridList[i].visible = false;
        }
        this.draggable = true;
        // this.dragBounds = new math.Rect(0, 0, this.width, this.height);
        this.on(fgui.Event.DRAG_END, this.onDragEnd, this);
        this.on(fgui.Event.DRAG_START, this.onDragStart, this);
        this.on(fgui.Event.DRAG_MOVE, this.onDragMove, this);
    }
    public removeClick(): void {
        this.draggable = false;
        this.rotate.visible = false;
        this.off(fgui.Event.DRAG_END, this.onDragEnd, this);
        this.off(fgui.Event.DRAG_START, this.onDragStart, this);
        this.off(fgui.Event.DRAG_MOVE, this.onDragMove, this);
    }
    public addClick(): void {
        this.draggable = true;
        this.rotate.visible = true;
        this.on(fgui.Event.DRAG_END, this.onDragEnd, this);
        this.on(fgui.Event.DRAG_START, this.onDragStart, this);
        this.on(fgui.Event.DRAG_MOVE, this.onDragMove, this);
    }
    private onDragMove(): void {
        // if (this.parent.parent.scaleX < 0.8) {
        //     return;
        // }
    }
    private onDragStart(): void {
        // if (this.parent.parent.scaleX < 0.8) {
        //     return;
        // }
        this.parent.setChildIndex(this, this.parent.numChildren - 1);
    }
    private onDragEnd(): void {
        // if (this.parent.parent.scaleX < 0.8) {
        //     return;
        // }
        this.onEnd();
    }
    private onEnd(): void {
        const arr: number[] = [];
        const dict: object = {};
        for (let i of ModelGameRobot.mesh()) {
            for (let j of i) {
                const dis = MathUtils.ins.getDistance(j[`x`], j[`y`], this.x, this.y);
                if (dis > ModelGameRobot.gridSize) { continue }
                arr.push(dis);
                dict[dis] = j;
            }
        }
        const jj = dict[Math.min(...arr)];
        if (!jj) {
            return;
        }
        this.x = jj[`x`];
        this.y = jj[`y`];
        this.fristgrid = `${jj[`i`]}-${jj[`j`]}`;
        this.planeTest(jj);
    }
    public get clientPos(): string {
        return `${this.fristgrid}+${this.state}`;
    }
    private planeTest(dict): void {
        const arr = ModelGameRobot.planeByrotate(this.state);
        this.gridArray = [];
        for (let i = 0; i < arr.length; i++) {
            const ii = arr[i].x + dict.i;
            const jj = arr[i].y + dict.j;
            this.gridArray.push(`${ii}-${jj}`);
            this.gridList[i].title = `${ii}-${jj}`;
        }
        EventMgr.emit(`planeTest`);
    }
    public set state(state: number) {
        this.ctrl_state.selectedIndex = state;
        switch (state) {
            case 0://上
                this.dragBounds = new math.Rect(this._px + this._ppx, this._py + this._ppy, this._pw, this._ph + ModelGameRobot.gridSize);
                break;
            case 1://下
                this.dragBounds = new math.Rect(this._px + this._ppx, this._py + this._ppy, this._pw, this._ph + ModelGameRobot.gridSize);
                break;
            case 2://左
                this.dragBounds = new math.Rect(this._px + this._ppx, this._py + this._ppy, this._pw + ModelGameRobot.gridSize, this._ph);
                break;
            case 3://右
                this.dragBounds = new math.Rect(this._px + this._ppx, this._py + this._ppy, this._pw + ModelGameRobot.gridSize, this._ph);
                break;
        }
        this.onEnd();
    }
    public setposTarget(px: number, py: number, ppx: number, ppy: number, pw: number, ph: number): void {
        this._px = px;
        this._py = py;
        this._ppx = ppx;
        this._ppy = ppy;
        this._pw = pw;
        this._ph = ph;
    }
    public get state(): number {
        return this.ctrl_state.selectedIndex;
    }
    public onRotateClick(): void {
        if (this.state == 3) {
            this.state = 0;
        } else {
            this.state += 1;
        }
    }
    public overlap(ar: any): void {
        for (let i of this.gridList) {
            if (ar.indexOf(i.title) != -1) {
                i.visible = true;
                i.setCtrlState(2);
                // i.alpha = 0.2
            } else {
                i.visible = false;
            }
        }
    }
}